#include "head.h"
/* Usage: */

bool initMysql(){
    MYSQL *sql = mysql_init(NULL);
    if(!sql){
        return false;
    }

    mysql_set_character_set(sql, "utf8mb4");
    if(!mysql_real_connect(sql, config->mysql_host, config->mysql_user, config->mysql_passwd, config->mysql_db, atoi(config->mysql_port), NULL, 0)){
        return false;
    }
    mysql_query(sql, "create table user(uid varchar(10) primary key not null,user_name varchar(255) not null,dir_id int not null,psw_hash varchar(255) not null,salt_value varchar(10) not null,islogin char not null,regis_date datetime,isDel char not null)");
    mysql_query(sql, "create table files(id int primary key auto_increment,file_name varchar(255) not null,uid varchar(255), pre_dir int not null, path varchar(255), file_type char not null, hash varchar(16), create_time datetime, finish char not null, offset bigint)");
    printf("%s\n", mysql_error(sql));

    mysql_close(sql);

    return true;
}

MYSQL *getsql(){
    MYSQL *sql = mysql_init(NULL);
    if(!sql){
        return NULL;
    }
    if(!mysql_real_connect(sql, config->mysql_host, config->mysql_user, config->mysql_passwd, config->mysql_db, atoi(config->mysql_port), NULL, 0)){
        printf("%s\n", mysql_error(sql));
        return NULL;
    }
    
    return sql;
}
